package com.yiren.backstage;

import android.database.sqlite.SQLiteDatabase;

import com.yiren.backstage.currency.constant.DbConstant;
import com.yiren.backstage.db.dao.controller.DatabaseContext;
import com.yiren.backstage.db.dao.DaoMaster;
import com.yiren.backstage.db.dao.DaoSession;

/**
 * Describe
 *
 * @author WRX
 * @date 2022/7/5
 */
public class App extends android.app.Application {

    private static App app;

    public App() {
        app = this;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        app = this;
    }

    public static App getInstance() {
        if (app == null) {
            synchronized (App.class) {
                if (app == null) {
                    app = new App();
                }
            }
        }
        return app;
    }

    private static DaoSession userSession;
    private static DaoSession gradeSession;
    private static DaoSession trajectSession;
    private static DaoSession cacheSession;

    public DaoSession getUserSession(boolean again) {
        if (again || userSession == null) {
            DatabaseContext databaseContext = new DatabaseContext(this, DbConstant.USER_DATA_URL.replace("user.db", ""));
            DaoMaster.DevOpenHelper devOpenHelper = new DaoMaster.DevOpenHelper(databaseContext, DbConstant.USER_DATA_NAME);
//            Database encryptedReadableDb = devOpenHelper.getEncryptedWritableDb("123456");
            SQLiteDatabase writableDatabase = devOpenHelper.getWritableDatabase();
            DaoMaster daoMaster = new DaoMaster(writableDatabase);
            userSession = daoMaster.newSession();
        }
        return userSession;
    }

    public DaoSession getGradeSession(boolean again) {
        if (again || gradeSession == null) {
            DatabaseContext databaseContext = new DatabaseContext(this, DbConstant.GRADE_DATA_URL.replace("grade.db", ""));
            DaoMaster.DevOpenHelper devOpenHelper = new DaoMaster.DevOpenHelper(databaseContext, DbConstant.GRADE_DATA_NAME);
//            Database encryptedReadableDb = devOpenHelper.getEncryptedWritableDb("123456");
            SQLiteDatabase writableDatabase = devOpenHelper.getWritableDatabase();
            DaoMaster daoMaster = new DaoMaster(writableDatabase);
            gradeSession = daoMaster.newSession();
        }
        return gradeSession;
    }

    public DaoSession getTrajectSession(boolean again) {
        if (again || trajectSession == null) {
            DatabaseContext databaseContext = new DatabaseContext(this, DbConstant.TRAJECT_DATA_URL.replace("traject.db", ""));
            DaoMaster.DevOpenHelper devOpenHelper = new DaoMaster.DevOpenHelper(databaseContext, DbConstant.TRAJECT_DATA_NAME);
//            Database encryptedReadableDb = devOpenHelper.getEncryptedWritableDb("123456");
            SQLiteDatabase writableDatabase = devOpenHelper.getWritableDatabase();
            DaoMaster daoMaster = new DaoMaster(writableDatabase);
            trajectSession = daoMaster.newSession();
        }
        return trajectSession;
    }

    public DaoSession getCacheSession() {
        if (cacheSession == null) {
            DatabaseContext databaseContext = new DatabaseContext(this, DbConstant.CACHE_DATA_URL);
            DaoMaster.DevOpenHelper devOpenHelper = new DaoMaster.DevOpenHelper(databaseContext, DbConstant.CACHE_DATA_NAME);
//            Database encryptedReadableDb = devOpenHelper.getEncryptedWritableDb("123456");
            SQLiteDatabase writableDatabase = devOpenHelper.getWritableDatabase();
            DaoMaster daoMaster = new DaoMaster(writableDatabase);
            cacheSession = daoMaster.newSession();
        }
        return cacheSession;
    }


}
